
var app = getApp();

const db = wx.cloud.database()
const eventshow = db.collection('eventshow')
// pages/add/add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileID: '', // 上传文件的ID
    cloudPath: '', // 定义文件的云端路径
    upload:'上传图片',
    type: ["文体艺术类", "创新创业类", "社会实践类", "志愿服务类", "学生工作类"]
  },
  pickerClass: function(e) {
    this.class = this.data.type[e.detail.value]
    this.setData({
      class: this.class
    })
  },
  inputName: function(e) {
    this.name = e.detail.value
    this.setData({
      name: this.name
    })
  },
  inputDate: function(e) {
    this.date = e.detail.value;
    this.setData({
      date: this.date
    })
  },
  inputCharge: function (e) {
    this.charge = e.detail.value;
    this.setData({
      charge: this.charge
    })
  },
  inputPlace: function(e) {
    this.place = e.detail.value;
    this.setData({
     place: this.place
    })
  },
  inputIntroduce: function(e) {
    this.introduce = e.detail.value;
    this.setData({
      introduce: this.introduce
    })
  },
  inputAward: function(e) {
    this.award = e.detail.value;
    this.setData({
      award: this.award
    })
  },
  uploadPhoto: function(e) {
    var that=this
    wx.chooseImage({
      count: 1, // 图片数量
      sizeType: ['compressed'], // 尺寸类型
      sourceType: ['album', 'camera'], // 图片来源
      success: function(res) {
        wx.showLoading({
          title: '上传中',
        })
        //获取图片临时路径
        const filePath = res.tempFilePaths[0]
        console.log("filePath:", filePath)
        var timelable = Date.parse(new Date());
        const cloudPath = '活动展/' + timelable + filePath.match(/\.[^.]+?$/)[0] //添加到活动展文件夹
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log(res)
            that.setData({
              fileID: res.fileID,
              cloudPath:cloudPath,
              upload:"更新图片"
              })
            wx.showToast({
              title: '上传成功',
              duration: 3000
            })
            //重定向到细节页面
            
          },
          fail: e => { // 调用失败时的回调函数
            console.error('[上传文件] 失败：', e);
            wx.showToast({ // 显示消息提示框
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => { // 调用完成时的回调函数
            wx.hideLoading() // 隐藏加载提示框
          }
        })
      }

    })

  },
  uploadForm:function(e){
   
    
    eventshow.add({
    data:{
    class: this.data.class,
      introduce: this.data.introduce,
      place: this.data.place,
      pic: this.data.fileID,
      attached: this.data.award,
     title: this.data.name,
     time:this.data.date,
      charge: this.data.charge,
     isHot:"false"
      },success: function (res) {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log(res)
        wx.switchTab({
          url: '../index/index'
        })
        console.log("yes")

      }
  })
  


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})